<?php
namespace app\index\model;
use think\Model;
use think\Db;

// 用户表模型
class User extends Model
{

	public function login($data){
		$captcha = new \think\captcha\Captcha();//验证码类
        if (!$captcha->check($data['code'])) {
            return 4;
        } 
		$user=Db::name('user')->where('username','=',$data['username'])->find();
		if($user){
			if($user['password'] == md5($data['password'])){
				session('username',$user['username']);
				session('uid',$user['uid']);
				return 3; //信息正确
			}else{
				return 2; //密码或用户名错误
			}
		}else{
			return 1; //用户不存在
		}
	}
	// 删除
	public function delink($id){
        if($this::destroy($id)){
            return 1;
        }else{
            return 2;
        }
    }
    // 添加
    public function addadmin($data){
    	if (empty($data)||!is_array($data)) {
    		return false;
    	}
    	if ($data['password']) {
    		$data['password']=md5($data['password']);
    	}
        $adminData=array();//重组数组user
        $adminData['username']=$data['username'];
        $adminData['password']=$data['password'];
    	if ($this->save($adminData)) {
            $groupAccess['uid']=$this->uid;
            $groupAccess['group_id']=$data['group_id'];
            db('group_access')->insert($groupAccess);//用户明细表
    		return true;
    	}else{
    		return false;
    	}
    }

  public function getadmin(){
    return $this::paginate(5,false,[
        'type'=>'bootstrap',
        'var_page' => 'page',
        ]);
   }
   public function saveadmin($data,$admins){
        if(!$data['username']){
            return 2;//管理员用户名为空
        }
        if(!$data['password']){
            $data['password']=$admins['password'];
        }else{
            $data['password']=md5($data['password']);
        }

        db('group_access')->where(array('uid'=>$data['uid']))->update(['group_id'=>$data['group_id']]);
        return $this::update(['username'=>$data['username'],'password'=>$data['password']],['uid'=>$data['uid']]);
    
    }













}
